package com.qiyadeng.wechat.roo.model;

import java.util.Calendar;
import java.util.List;

import org.springframework.roo.addon.dbre.RooDbManaged;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;

@RooJavaBean
@RooToString
@RooJpaActiveRecord(versionField = "", table = "USER_BINDING")
@RooDbManaged(automaticallyDelete = true)
public class UserBinding {

	//查询用户的绑定情况
	public static UserBinding getUserBindingByWeChatId(String wehchatid){
		List<UserBinding> userBinds = entityManager().createQuery("select o from UserBinding o where o.wechatUuid=?1",UserBinding.class)
				.setParameter(1, wehchatid)
				.getResultList();
		if(userBinds.size() > 0){
			return userBinds.get(0);
		}
		return null;
	}
	
	//建立绑定关系
	public static void saveUserBinding(String wechatid, String phone){
		UserBinding u = new UserBinding();
		u.setBindingTime(Calendar.getInstance());
		u.setPhone(phone);
		u.setWechatUuid(wechatid);
		u.persist();
	}
}
